Skip to content

Comments

refactor: use dependency injection for InsightsBookingService#22790

Merged
eunjae-lee merged 10 commits intomainfrom
devin/insights-booking-service-di-1753792364
Aug 5, 2025
Merged

refactor: use dependency injection for InsightsBookingService#22790
eunjae-lee merged 10 commits intomainfrom
devin/insights-booking-service-di-1753792364

Conversation

@eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Jul 29, 2025

refactor: use dependency injection for InsightsBookingService

Summary

This PR refactors InsightsBookingService to use dependency injection, following the exact same pattern established for InsightsRoutingService in PR #22677. The changes include:

  • Renamed InsightsBookingServiceInsightsBookingBaseService
  • Created new DI interface InsightsBookingService with create() method
  • Added DI module and container using @evyweb/ioctopus
  • Restored createInsightsBookingService helper that now uses getInsightsBookingService() DI container internally
  • Updated all 12 tRPC endpoint handlers to continue using createInsightsBookingService(ctx, input)
  • Updated test files and documentation to reflect the new pattern

The refactoring maintains the same public API while enabling dependency injection for better testability and modularity. All existing functionality should be preserved.

Review & Testing Checklist for Human

  • Test all insights tRPC endpoints work correctly - Verify booking KPI stats, event trends, popular events, member stats, ratings, and CSV export still function properly
  • Verify DI pattern consistency - Ensure the implementation exactly matches the InsightsRoutingService DI pattern from PR refactor: use dependency injection for InsightsRoutingService #22677 (same tokens, module structure, container setup)
  • Check all 12 tRPC handlers - Confirm bookingKPIStats, eventsByHourStats, popularEvents, memberStats, etc. all use createInsightsBookingService(ctx, input) correctly
  • Verify no import/export issues - Check that all file renames were handled correctly and no circular dependencies were introduced
  • Test integration points - Verify the DI container properly injects dependencies and the service instantiation works as expected

Recommended test plan: Navigate to the insights dashboard and verify that all charts, stats, and data exports load correctly with the same data as before the refactoring. Pay special attention to date filtering and team/user scoping functionality.


Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    subgraph "tRPC Router"
        Router["packages/features/insights/<br/>server/trpc-router.ts"]:::major-edit
    end
    
    subgraph "Service Layer"
        OldService["packages/lib/server/service/<br/>insightsBooking.ts<br/>(DELETED)"]:::major-edit
        BaseService["packages/lib/server/service/<br/>InsightsBookingBaseService.ts<br/>(renamed from above)"]:::major-edit
        DIService["packages/lib/server/service/<br/>InsightsBookingDIService.ts<br/>(NEW)"]:::major-edit
    end
    
    subgraph "DI Infrastructure"
        Tokens["packages/lib/di/tokens.ts"]:::minor-edit
        Module["packages/lib/di/modules/<br/>insights-booking.ts<br/>(NEW)"]:::major-edit
        Container["packages/lib/di/containers/<br/>insights-booking.ts<br/>(NEW)"]:::major-edit
    end
    
    subgraph "Tests & Docs"
        Tests["packages/lib/server/service/<br/>__tests__/insightsBooking.integration-test.ts"]:::minor-edit
        Docs["packages/features/insights/<br/>HOW_TO_ADD_BOOKING_CHARTS.md"]:::minor-edit
    end
    
    Router -->|"uses createInsightsBookingService()"| Container
    Container -->|"creates"| DIService
    DIService -->|"instantiates"| BaseService
    Module -->|"binds"| DIService
    Container -->|"loads"| Module
    Tests -->|"imports"| BaseService
    
    subgraph Legend
        L1["Major Edit"]:::major-edit
        L2["Minor Edit"]:::minor-edit
        L3["Context/No Edit"]:::context
    end

classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB
classDef context fill:#FFFFFF
Loading

Notes

  • This refactoring follows the established dependency injection pattern from PR refactor: use dependency injection for InsightsRoutingService #22677 for InsightsRoutingService
  • The createInsightsBookingService helper function was restored per feedback and now uses getInsightsBookingService internally
  • All 12 tRPC endpoints continue using the same API: createInsightsBookingService(ctx, input)
  • Integration tests pass, but full end-to-end testing of tRPC endpoints is recommended
  • Type checking shows unrelated lockedTimeZone errors in other files, which were explicitly skipped per user instruction

Link to Devin run: https://app.devin.ai/sessions/75ca69a009b8483a9b6dd4a1e472f66f
Requested by: @eunjae-lee

- Add DI tokens for InsightsBookingService and module
- Rename InsightsBookingService to InsightsBookingBaseService
- Create InsightsBookingService DI interface with create method
- Add DI module and container for InsightsBookingService
- Update tRPC router to use getInsightsBookingService DI container
- Update test file to use InsightsBookingBaseService
- Update documentation to reflect new DI pattern

Follows the same dependency injection pattern established for InsightsRoutingService in PR #22677

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 29, 2025

Walkthrough

This change introduces a dependency injection (DI) architecture for the insights booking service. A new DI container and module are added for the insights booking service, with corresponding DI tokens. The service instantiation in the tRPC router is refactored to use the DI container via a new getInsightsBookingService function, replacing direct instantiation and the previous helper. The core service class is renamed from InsightsBookingService to InsightsBookingBaseService, and a new factory/service class (InsightsBookingService) is introduced to create instances of the base service. Documentation and tests are updated to reflect these structural changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Complexity label: Moderate
Rationale:
The review involves changes across several files, including the introduction of a new DI pattern, refactoring of service instantiation, class renaming, and updates to documentation and tests. While the architectural shift is significant, the individual changes are straightforward and localized, with no complex new logic or algorithms added.

Possibly related PRs

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between beffac6 and f99de7d.

📒 Files selected for processing (3)
  • packages/features/insights/server/trpc-router.ts (2 hunks)
  • packages/lib/server/service/InsightsBookingBaseService.ts (1 hunks)
  • packages/lib/server/service/__tests__/InsightsBookingService.integration-test.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/lib/server/service/tests/InsightsBookingService.integration-test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/lib/server/service/InsightsBookingBaseService.ts
  • packages/features/insights/server/trpc-router.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch devin/insights-booking-service-di-1753792364

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@keithwillcode keithwillcode added consumer core area: core, team members only labels Jul 29, 2025
…ally

- Keep createInsightsBookingService helper function for cleaner API
- Use getInsightsBookingService DI container internally
- Maintain same function signature and behavior
- All existing calls continue to work unchanged

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
@vercel
Copy link

vercel bot commented Jul 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Aug 5, 2025 8:06am
cal-eu ⬜️ Ignored (Inspect) Aug 5, 2025 8:06am

- Rename insightsBookingBase.ts to InsightsBookingBaseService.ts
- Rename insightsBookingDI.ts to InsightsBookingDIService.ts
- Update all import statements to use new file names
- Maintain existing handler functionality using createInsightsBookingService(ctx, input)

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
@devin-ai-integration devin-ai-integration bot marked this pull request as ready for review July 29, 2025 13:55
@graphite-app graphite-app bot requested a review from a team July 29, 2025 13:55
@graphite-app
Copy link

graphite-app bot commented Jul 29, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (07/29/25)

1 reviewer was added to this PR based on Keith Williams's automation.

"Add ready-for-e2e label" took an action on this PR • (08/04/25)

1 label was added to this PR based on Keith Williams's automation.

@dosubot dosubot bot added insights area: insights, analytics 💻 refactor labels Jul 29, 2025
@eunjae-lee eunjae-lee marked this pull request as draft July 29, 2025 14:09
@eunjae-lee eunjae-lee marked this pull request as ready for review July 29, 2025 14:21
@emrysal emrysal marked this pull request as draft July 31, 2025 00:51
hbjORbj
hbjORbj previously approved these changes Aug 4, 2025
Copy link
Contributor

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it :D You have a merge conflict though. Feel free to ping me once more for approval

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

E2E results are ready!

Copy link
Contributor

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

consumer core area: core, team members only insights area: insights, analytics ready-for-e2e 💻 refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants